<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\BaseController;

use think\facade\Cache;
use think\facade\Session;
use think\facade\Request;
use think\facade\Db;

use think\facade\View;

/**
 * 后台公用基础控制器
 * Class Base
 * @package app\admin\controller
 */
class Base extends BaseController
{

	protected $auth;
	protected $module;
	protected $controller;
	protected $action;
	
	/**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct()
    {
		$this->module     = app('http')->getName();
        $this->controller = Request::controller();
        $this->action     = Request::action();

		$this->auth = new \Auth();

		$this->baseInfo();
		$this->checkAuth();
		$this->adminMenu();


    }

    /**
     * 获取基本信息
     * @return bool
     */
    protected function baseInfo()
    {
		$website_info = Db::name('system_item')->where('system_group_id',1)->column('value','field');
		$admin_info = Db::name('admin')->where('id',session('admin.id'))->find();

		View::assign('website_info',$website_info);
		View::assign('admin_info',$admin_info);
    }

    /**
     * 权限检查
     * @return bool
     */
    protected function checkAuth()
    {

        if (!Session::has('admin.id')) {
            $this->error('请先登陆',url('admin/login/index'));
        }


        // 排除权限
        $not_check = ['admin/Index/index', 'admin/Index/recycle', 'admin/AdminGroup/getjson'];

        if (!in_array($this->module .'/' .$this->controller . '/' .$this->action, $not_check)) {
            $admin_id = Session::get('admin.id');
            if (!$this->auth->check($this->module . '/' . $this->controller . '/' . $this->action, $admin_id) && $admin_id != 1) {
                $this->error('没有权限');
            }
        }


    }
    /**
     * 菜单
     * @return bool
     */
    protected function adminMenu()
    {
		//获取顶级 ID 
		if(Cache::has('auth_ids')) {
			$auth_ids = Cache::get('auth_ids');
		}else{
			$auth_ids = Db::name('admin_authrule')->column('pid', 'id');
			Cache::set('auth_ids',$auth_ids);
		}	

		$menu_id = Db::name('admin_authrule')->where('name', $this->module .'/' .$this->controller . '/' .$this->action)->value('id');

		$menu_pid = array($menu_id);

		if($menu_id == 0){
			View::assign('menu_topid',0);
		}else{
			$top_id = $menu_id;
			while($auth_ids[$top_id] != 0){
				$top_id = $auth_ids[$top_id];
				array_push($menu_pid,$top_id);
			}
			//获取顶级菜单，用于顶部的选中样式
			View::assign('menu_topid',$top_id);
		}

		//获取所有上级菜单，用于当前选中样式
		View::assign('menu_pid',$menu_pid);


		if(Cache::has('admin_menu')) {
			$admin_menu = Cache::get('admin_menu');
		}else{
			$admin_menu	= [];

			$admin_id = Session::get('admin.id');

			$auth_rule_list = Db::name('admin_authrule')->where('status', 1)->order('sort desc, id asc')->select();

			foreach ($auth_rule_list as $value) {
				if ($this->auth->check($value['name'], $admin_id) || $admin_id == 1) {
					$admin_menu[] = $value;
				}
			}
	
			$admin_menu = !empty($admin_menu) ? list_to_tree($admin_menu) : [];

			Cache::set('admin_menu',$admin_menu);
		}
		View::assign('admin_menu',$admin_menu);

	}


}